package com.example.demo;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyPagerAdapter extends RecyclerView.Adapter<MyPagerAdapter.ViewHolder> {

    // 页面数据 - 颜色和文字
    private int[] colors = {Color.parseColor("#FFB6C1"), // 浅粉色
            Color.parseColor("#87CEEB"), // 浅蓝色
            Color.parseColor("#98FB98"), // 浅绿色
            Color.parseColor("#FFD700")  // 金色
    };

    private String[] texts = {"第一页\n\n欢迎使用ViewPager!", "第二页\n\n左右滑动切换页面", "第三页\n\n很简单对吧？", "第四页\n\n不需要Fragment也能用"};

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建页面布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(colors[position], texts[position]);
    }

    @Override
    public int getItemCount() {
        return colors.length;
    }

    // ViewHolder类
    static class ViewHolder extends RecyclerView.ViewHolder {
        private View rootView;
        private TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            textView = itemView.findViewById(R.id.textView);
        }

        public void bind(int color, String text) {
            rootView.setBackgroundColor(color);
            textView.setText(text);
        }
    }
}